import 'dart:io';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:fsharing/core/constants/app_contstants.dart';
import 'package:fsharing/core/models/access_token.dart';
import 'package:fsharing/core/models/me.dart';
import 'package:fsharing/core/models/reg_new_qr.dart';
import 'package:fsharing/core/models/user_realname_query.dart';
import 'package:fsharing/core/services/dio_interceptor.dart';
import 'package:fsharing/core/viewmodels/app_model.dart';

class Auth {
  static const clientId = 'lingling9.app';
  static const clientSecret = 'tmt.lingling9.app';

  final Dio _authClient;

  Auth(AppStoreModel appStoreModel, AppLoginModel appLoginModel)
      : _authClient = Dio()
          ..options.baseUrl = auth_base_url
          ..options.connectTimeout = 5000
          ..options.sendTimeout = 5000
          ..options.receiveTimeout = 5000
          ..interceptors.add(DioInterceptor(appStoreModel, appLoginModel))
          ..interceptors.add(LogInterceptor(requestBody: true, responseBody: true)) {
    debugPrint('$this init');
  }

  /// 获取AccessToken
  Future<AccessToken> oauthToken(String username, String password) async {
    var params = {
      'grant_type': 'password',
      'client_id': clientId,
      'client_secret': clientSecret,
      'username': username,
      'password': password,
    };
    var response = await _authClient.post('/s/oauth/token', data: FormData.fromMap(params));
    return AccessToken.fromJson(response.data);
  }

  /// 个人信息
  Future<Me> me(CancelToken cancelToken) async {
    var params = {'client_id': clientId};
    var formData = FormData.fromMap(params);
    var response = await _authClient.post('/s/me', data: formData, cancelToken: cancelToken);
    return Me.fromJson(response.data);
  }

  /// 验证码获取
  Future reqCode(String type, String phone) async {
    var params = {'clientId': clientId, 'funcPoint': type, 'mobile': phone};
    var response = await _authClient.post('/s/api/reg/code', data: params);
    return response.data;
  }

  /// 用户实名认证申请结果查询接口
  Future<UserRealnameQuery> userRealnameQuery() async {
    var response = await _authClient.post('/s/api/user/realname/query');
    return UserRealnameQuery.fromJson(response.data);
  }

  /// 用户实名认证申请记录删除接口
  Future userRealnameDelete(int rlId) async {
    var response = await _authClient.post('/s/api/user/realname/delete/$rlId');
    return response.data;
  }

  /// 提交用户实名认证资料
  Future userRealname(Map<String, dynamic> params) async {
    // params['cardNo'] = cardNo;
    // params['cardType'] = cardType;
    // params['name'] = name;
    // params['checkCode'] = checkCode;
    // params['frontFile'] = _frontFile;
    // params['backFile'] = _frontFile;
    params['clientId'] = clientId;

    var front = params['frontFile'] as File;
    var back = params['backFile'] as File;
    if (front == null || back == null) {
      throw '未拍摄身份证';
    }
    params['frontFile'] = await MultipartFile.fromFile(front.path, filename: 'frontFile.png');
    params['backFile'] = await MultipartFile.fromFile(back.path, filename: 'backFile.png');

    debugPrint('$params');
    var response = await _authClient.post('/s/api/user/realname', data: FormData.fromMap(params));
    return response.data;
  }

  /// 获取推荐二维码
  Future<String> userQr() async {
    var params = {'clientId': clientId};
    var response = await _authClient.post('/s/api/user/qr', data: params);
    return response.data;
  }

  /// 用户注册接口
  Future<RegNewQr> regNew(Map<String, dynamic> params) async {
    // params['checkCode'] = checkCode;
    // params['inviteCode'] = inviteCode;
    // params['mobile'] = mobile;
    // params['password'] = password;
    // params['password1'] = password1;
    // params['username'] = username;
    params['clientId'] = clientId;
    var response = await _authClient.post('/s/api/reg/new', data: params);
    return RegNewQr.fromJson(response.data);
  }

  /// 用户扫码注册接口
  Future<RegNewQr> regNewQr(String encodedClientId, String invitedCode) async {
    var params = {'encodedClientId': encodedClientId, 'invitedCode': invitedCode};
    var response = await _authClient.post('/s/api/reg/new/qr', data: params);
    return RegNewQr.fromJson(response.data);
  }

  /// 重置密码
  Future regPasswordReset(Map<String, dynamic> params) async {
    // params['checkCode'] = checkCode;
    // params['mobile'] = mobile;
    // params['newPassword'] = newPassword;
    // params['username'] = username;
    params['clientId'] = clientId;
    var response = await _authClient.post('/s/api/reg/password/reset', data: params);
    return response.data;
  }

  /// 变更用户头像
  Future userAvatarUpdate(File newAvatar) async {
    var params = <String, dynamic>{};
    params['clientId'] = clientId;
    params['newAvatar'] = await MultipartFile.fromFile(newAvatar.path, filename: 'newAvatar.png');
    var formData = FormData.fromMap(params);
    var response = await _authClient.post('/s/api/user/avatar/update', data: formData);
    return response.data;
  }

  /// 重新绑定手机号码
  Future userMobileUpdate(Map<String, dynamic> params) async {
    // params['mobile'] = mobile;
    // params['checkCode'] = checkCode;
    // params['verifyCode'] = verifyCode;
    params['clientId'] = clientId;
    var response = await _authClient.post('/s/api/user/mobile/update', data: params);
    return response.data;
  }

  /// 变更用户昵称
  Future userNicknameUpdate(String newNickname) async {
    var params = <String, dynamic>{};
    params['newNickName'] = newNickname;
    params['clientId'] = clientId;
    var response = await _authClient.post('/s/api/user/nickname/update', data: params);
    return response.data;
  }

  /// 修改登录密码接口
  Future userPasswordUpdate(Map<String, dynamic> params) async {
    // params['oldPassword'] = mobile;
    // params['newPassword'] = checkCode;
    params['clientId'] = clientId;
    var response = await _authClient.post('/s/api/user/password/update', data: params);
    return response.data;
  }
}
